﻿using System;
using System.Collections.Generic;


namespace DailyFx.UI
{
	/// <summary>
    ///		Generic Service Locator interface, 
	///		which is used to retrieve services (identified by type and optional name) .
    /// </summary>
    public interface IServiceLocator : IServiceProvider
    {
        /// <summary>
		/// Get an service of the given named <paramref name="type"/>.
        /// </summary>
		object GetService (Type type, string key  );

        /// <summary>
		///		Gets all services of the given <paramref name="type"/>  currently registered.
        /// </summary>
        IEnumerable<object> GetServices ( Type type );

    }

	static class ServiceLocatorExtension
	{
		/// <summary>
		///		Gets an service of the given named <typeparamref name="TService"/>.
		/// </summary>
		public static TService GetService<TService> ( this IServiceLocator locator, string key = null )
		{
			return (TService) locator.GetService ( typeof(TService), key );
		}

		/// <summary>
		///		Gets all instances of the given <typeparamref name="TService"/> currently registered.
		/// </summary>
		public static IEnumerable<TService> GetServices<TService>( this IServiceLocator locator)
		{
			foreach ( object s in locator.GetServices(typeof(TService)) )
			{
				yield return (TService)s ;
			}
		}
	}
}
